DOM = (document.getElementById) ? 1 : 0;
Opera = (navigator.userAgent.indexOf("Opera") > -1) ? 1 : 0;
IE = (navigator.userAgent.indexOf("MSIE") > -1) ? 1 : 0;
IE = IE && !Opera;
IE4 = (document.all) ? 1 : 0;
IE4 = IE4 && IE && !DOM;
using_status = false;

function get_element_by_id(name) {
  if (!IE4) {
    return document.getElementById(name);
  } else {
    return document.all(name);
  }
}

function t_select(){
	node_id = this.id.substring(0,this.id.indexOf('_'));
	prefix = node_id + '_n';

  name_nod_l = prefix + '_l';
  name_nod_m = prefix;
  name_nod_r = prefix + '_r';

  t_select_element(name_nod_l);
  t_select_element(name_nod_m);
  t_select_element(name_nod_r);

	update_status(node_id);
}

function update_status(node_id) {
  prefix = node_id + '_n';
  element = get_element_by_id(prefix);
  status_set(element.alt + " this node");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
function status_set(status) {
  if (using_status) {
    window.status = status;
  }
}

function remove_any_existing_suffix(str) {
  if (str == null) return "";

  idx = str.lastIndexOf(".");
  branch = str.substring(0,idx);

  sel_idx = branch.lastIndexOf("_sel");
  if (sel_idx > -1) {
    len = branch.length;
    return branch.substring(0,len - 4) + ".png";
  }

  sel_idx = branch.lastIndexOf("_pre");
  if (sel_idx > -1) {
    len = branch.length;
    return branch.substring(0,len - 4) + ".png";
  }

  return str;
}

function t_deselect(node_id) {
  prefix = node_id+'_n';
  name_nod_m = prefix;
  name_nod_l = prefix + '_l';
  name_nod_r = prefix + '_r';

  t_deselect_element(name_nod_l);
  t_deselect_element(name_nod_m);
  t_deselect_element(name_nod_r);

  status_set("");
}

function t_deselect_element(name) {
	element = get_element_by_id(name);
  new_image = remove_any_existing_suffix(element.src);
  element.src = new_image;
}

function t_select_element(name) {
  element = get_element_by_id(name);
  new_image = remove_any_existing_suffix(element.src);
  new_image = append_selected_suffix(new_image);
  element.src = new_image;
}

//function t_release_compo(table_id, node_id) {
function t_release_compo(){
	node_id = this.id.substring(0,this.id.indexOf('_'));
  name_nod_m = node_id + '_n';
  name_nod_l = prefix + '_l';
  name_nod_r = prefix + '_r';

  t_deselect_element(name_nod_l);
  t_deselect_element(name_nod_m);
  t_deselect_element(name_nod_r);
}


function t_press_compo() {
	node_id = this.id.substring(0,this.id.indexOf('_'));
  name_nod_m = node_id + '_n';
  name_nod_l = prefix + '_l';
  name_nod_r = prefix + '_r';

  t_press_obj(name_nod_l);
  t_press_obj(name_nod_m);
  t_press_obj(name_nod_r);
}

function t_press_obj(name) {
  element = get_element_by_id(name);

  new_image = remove_any_existing_suffix(element.src);
  new_image = append_pressed_suffix(new_image);

  element.src = new_image;
}

function append_pressed_suffix(str) {
  idx = str.lastIndexOf(".");
  branch = str.substring(0,idx);

  return branch + "_pre.png";
}

function append_selected_suffix(str) {
  idx = str.lastIndexOf(".");
  branch = str.substring(0,idx);

  return branch + "_sel.png";
}

function t_select_element_and_set_status(name, status) {
  t_select_element(name);

  status_set(status);
}

function t_deselect_element_and_status(name) {
  t_deselect_element(name);
  status_set("");
}

//function tog_t_menu(table_id, node_id) {
function tog_t_menu(){
  //prefix = 't_' + table_id + '_' + node_id;
	node_id = this.id.substring(0,this.id.indexOf('_'));
	prefix = node_id;
	
  name_son = prefix + '_son';
  name_nol = prefix + '_n_l';
  name_nod = prefix + '_n';
  name_nor = prefix + '_n_r';
  name_fle = prefix + '_f_l';
  name_fmi = prefix + '_f_m';
  name_fri = prefix + '_f_r';
  name_fmc = prefix + '_f_m_colr';
  name_fld = prefix + '_f';

  element_son = get_element_by_id(name_son);
  element_nol = get_element_by_id(name_nol);
  element_nod = get_element_by_id(name_nod);
  element_nor = get_element_by_id(name_nor);
  element_fle = get_element_by_id(name_fle);
  element_fmi = get_element_by_id(name_fmi);
  element_fri = get_element_by_id(name_fri);
  element_fmc = get_element_by_id(name_fmc);
  element_fld = get_element_by_id(name_fld);

  src_leaf = get_leaf(element_nod.src);

  //if (element_son == null) {
    //return;
  //}

  //if (element_son.style == null) {
    //return;
  //}

  if (element_son.style.display == "none") {
    element_son.style.display = "block";

    element_nod.src = image_directory + get_leaf(element_nod.src).replace("_pl", "_mi");

    element_fle.src = image_directory + get_leaf(element_fle.src).replace("_cl_", "_op_");
    element_fmi.src = image_directory + get_leaf(element_fmi.src).replace("_cl_", "_op_");
    element_fri.src = image_directory + get_leaf(element_fri.src).replace("_cl_", "_op_");

    element_fmc.style.background = "#808080";
    // element_fld.value = "+";
    element_nol.alt = "Collapse";
    element_nod.alt = "Collapse";
    element_nor.alt = "Collapse";
  } else {
    element_son.style.display = "none";

    element_nod.src = image_directory + get_leaf(element_nod.src).replace("_mi", "_pl");

    element_fle.src = image_directory + get_leaf(element_fle.src).replace("_op_", "_cl_");
    element_fmi.src = image_directory + get_leaf(element_fmi.src).replace("_op_", "_cl_");
    element_fri.src = image_directory + get_leaf(element_fri.src).replace("_op_", "_cl_");

    element_fmc.style.background = "";
    // element_fld.value = "-";
    element_nol.alt = "Expand";
    element_nod.alt = "Expand";
    element_nor.alt = "Expand";
  }

  update_status(node_id);

  //for (var i = get_r_total(table_id); --i >= 0; ) {
    t_deselect(node_id);
  //}

  //t_deselect(table_id, node_id);
}


function set_t_menu(node_id, new_value) {
  name_fld = node_id + '_f';

  element_fld = get_element_by_id(name_fld);

  element_fld.value = new_value;
}
/*
function collapse_all_nodes(t_id) {
  status_set("[Closing all nodes]");
  using_status = false;
  tog_matching_nodes_in_range(t_id, "+", 0, 199);
  status_free_and_clear();
}

function expand_all_nodes(t_id) {
  status_set("[Opening all nodes]");
  using_status = false;
  tog_matching_nodes_in_range(t_id, "-", 0, 199);
  status_free_and_clear();
}

function open_as_far_as(t_id, level) {
  status_set("[Opening nodes as far as level " + level + "]");
  using_status = false;

  tog_matching_nodes_in_range(t_id, "-", 0, level);
  tog_matching_nodes_in_range(t_id, "+", level, 199);

  status_free_and_clear();
}

function t_d_c(t_id) {
  status_set("[Toggling descendant controls]");
  using_status = false;

  var action = false;
  for (var i = get_r_total(t_id); --i >= 0; ) {
    e_id = 't_' + t_id + '_op_descendants_of_t_' + t_id + "_" + i + "_x";
    toggle_element_visibility(e_id);
    e_id = 't_' + t_id + '_close_descendants_of_t_' + t_id + "_" + i + "_x";
    toggle_element_visibility(e_id);
  }

  status_free_and_clear();
}

function toggle_element_visibility(e_id) {
  element_fld = get_element_by_id(e_id);
  if (element_fld != null) {
    if (element_fld.style.display != "none") {
      element_fld.style.display = "none";
    } else {
      element_fld.style.display = "";
    }
  }
}

function open_descendants_of(node) {
  tree = get_t_from_n_name(node);
  row = get_r_from_n_name(node);
  max_row = get_r_total(tree);

  status_set("[Opening all descendants]");
  using_status = false;

  prefix = 't_' + tree + '_' + row;
  level_id = prefix + '_l';
  element_level = get_element_by_id(level_id);
  if (element_level != null) {
    initial_level = element_level.value;
    initial_row = ++row;
    do {
      prefix = 't_' + tree + '_' + row;
      level_id = prefix + '_l';
      element_level = get_element_by_id(level_id);
      if (element_level != null) {
        current_level = element_level.value;
        if ((current_level > initial_level) || (initial_row == row)) {
          tog_node(tree, row, "-");
        }
      }
      row++;
    } while ((row <= (initial_row + 2)) || ((current_level > initial_level) && (row < max_row)));
  }

  row = get_r_from_n_name(node);
  level_id = prefix + '_l';
  element_level = get_element_by_id(level_id);
  if (element_level != null) {
    prefix = 't_' + tree + '_' + row;
    level_id = prefix + '_l';
    element_level = get_element_by_id(level_id);
    if (element_level != null) {
      tog_node(tree, row, "-");
    }
  }

  status_free_and_clear();
}

function close_descendants_of(node) {
  status_set("[Closing all descendants]");
  using_status = false;

  tree = get_t_from_n_name(node);
  row = get_r_from_n_name(node);
  max_row = get_r_total(tree);

  prefix = 't_' + tree + '_' + row;
  level_id = prefix + '_l';
  element_level = get_element_by_id(level_id);
  if (element_level != null) {
    initial_level = element_level.value;
    initial_row = ++row;
    do {
      prefix = 't_' + tree + '_' + row;
      level_id = prefix + '_l';
      element_level = get_element_by_id(level_id);
      if (element_level != null) {
        current_level = element_level.value;
        if ((current_level > initial_level) || (row == initial_row)) {
          tog_node(tree, row, "+");
        }
      }
      row++;
    } while ((row <= (initial_row + 2)) || ((current_level > initial_level) && (row < max_row)));
  }

  status_free_and_clear();
}
*/
function status_free() {
  using_status = true;
}

function status_free_and_clear() {
  status_free();
  status_set("");
}

/*
function initially_collapse_all_nodes(t_id, min, max) {
  tog_matching_nodes_in_range(t_id, "i", min, max);
}
*/

function get_leaf(str) {
  idx = str.lastIndexOf("/");
  while (idx > -1) {
    str = str.substring(idx + 1);
    idx = str.lastIndexOf("/");
  }

  return str;
}

function deselect_if_s(str) {
  idx = str.lastIndexOf(".");
  branch = str.substring(0,idx);

  sel_idx = branch.lastIndexOf("_sel");
  if (sel_idx > -1) {
    len = branch.length;
    return branch.substring(0,len - 4) + ".png";
  }

  return null;
}

function select_if_deselected(str) {
  idx = str.lastIndexOf(".");
  branch = str.substring(0,idx);

  sel_idx = branch.lastIndexOf("_sel");
  if (sel_idx <= -1) {
    return branch + "_sel.png";
  }

  return null;
}

function t_restore_from_ff(t_id) {
  var action = false;
  for (var i = get_r_total(t_id); --i >= 0; ) {
    prefix = 't_' + t_id + '_' + i;
    fld_id = prefix + '_f';
    element_fld = get_element_by_id(fld_id);
    if (element_fld != null) {
      id_son = prefix + '_son';
      element_son = get_element_by_id(id_son);
      if (element_son != null) {
        if ((element_son.style.display != "none") && (element_fld.value == "-")) {
          tog_t_menu(t_id, i);
          action = true;
        } else if ((element_son.style.display == "none") && (element_fld.value == "+")) {
          tog_t_menu(t_id, i);
          action = true;
        }
      }
    }
  }

  status_free();

  return action;
}

function tog_matching_nodes_in_range(t_id, matchstring, min, max) {
  for (var i = get_r_total(t_id); --i >= 0; ) {
    prefix = 't_' + t_id + '_' + i;
    fld_id = prefix + '_f';
    element_fld = get_element_by_id(fld_id);
    if (element_fld != null) {
      if (element_fld.value == matchstring) {
        indent_id = prefix + '_l';
        element_level = get_element_by_id(indent_id);
        if (element_level != null) {
          if (element_level.value >= min) {
            if (element_level.value <= max) {
              tog_t_menu(t_id, i);
            }
          }
        }
      }
    }
  }
}

function tog_node(tree, row, matchstring) {
  prefix = 't_' + tree + '_' + row;
  fld_id = prefix + '_f';
  element_fld = get_element_by_id(fld_id);
  if (element_fld != null) {
    if (element_fld.value == matchstring) {
      tog_t_menu(tree, row);
    }
  }
}

function set_up_nodes_not_in_range(t_id, matchstring, min, max) {
  for (var i = get_r_total(t_id); --i >= 0; ) {
    prefix = 't_' + t_id + '_' + i;
    fld_id = prefix + '_f';
    element_fld = get_element_by_id(fld_id);
    if (element_fld != null) {
      if (element_fld.value == matchstring) {
        indent_id = prefix + '_l';
        element_level = get_element_by_id(indent_id);
        if (element_level != null) {
          if ((element_level.value < min) || (element_level.value > max)) {
            set_t_menu(t_id, i, "+");
          }
        }
      }
    }
  }
}

function get_r_total(t_id) {
  count_name = 't_' + t_id + '_r_total';
  element_count = get_element_by_id(count_name);
  if (element_count != null) {
    return element_count.value;
  }

  return 100;
}

function open_named_node(name) {
  tree = get_t_from_n_name(name);
  row = get_r_from_n_name(name);

  prefix = 't_' + tree + '_' + row;

  fld_id = prefix + '_f';
  element_fld = get_element_by_id(fld_id);

  if (element_fld.value != "+") {
    tog_t_menu(tree, row);
  }
}

function get_t_from_n_name(name) {
  t_name = "t_" + name;
  element_tree = get_element_by_id(t_name);

  return element_tree.value;
}

function get_r_from_n_name(name) {
  r_name = "r_" + name;
  element_row = get_element_by_id(r_name);

  return element_row.value;
}

function preload_image(img) {
  (new Image()).src = image_directory + img;
}

function preload_png(png) {
  (new Image()).src = image_directory + png + ".png";
}

function t_preload_images() {
  preload_png("t_n_m_1_mi");
  preload_png("t_n_m_1_pl");
  preload_png("t_n_m_9_mi");
  preload_png("t_n_m_9_pl");

  preload_png("t_col_all_sel");
  preload_png("t_exp_all_sel");
  preload_png("t_n_l_sel");
  preload_png("t_n_m_1_mi_sel");
  preload_png("t_n_m_1_pl_sel");
  preload_png("t_n_m_9_mi_sel");
  preload_png("t_n_m_9_pl_sel");
  preload_png("t_op_as_far_as_sel");
  preload_png("t_n_r_sel");

  preload_png("t_col_all_pre");
  preload_png("t_exp_all_pre");
  preload_png("t_n_l_pre");
  preload_png("t_n_m_1_mi_pre");
  preload_png("t_n_m_1_pl_pre");
  preload_png("t_n_m_9_mi_pre");
  preload_png("t_n_m_9_pl_pre");
  preload_png("t_op_as_far_as_pre");
  preload_png("t_n_r_pre");
}


//added by kernel0722@gmail.com
////////////////////////////////////////////////////////////////////////////////////////////////////




function set_node_state(node_id, expand) {
  prefix = node_id;

  name_son = prefix + '_son';
  name_nol = prefix + '_n_l';
  name_nod = prefix + '_n';
  name_nor = prefix + '_n_r';
  name_fle = prefix + '_f_l';
  name_fmi = prefix + '_f_m';
  name_fri = prefix + '_f_r';
  name_fmc = prefix + '_f_m_colr';
  name_fld = prefix + '_f';

  element_son = get_element_by_id(name_son);
  element_nol = get_element_by_id(name_nol);
  element_nod = get_element_by_id(name_nod);
  element_nor = get_element_by_id(name_nor);
  element_fle = get_element_by_id(name_fle);
  element_fmi = get_element_by_id(name_fmi);
  element_fri = get_element_by_id(name_fri);
  element_fmc = get_element_by_id(name_fmc);
  element_fld = get_element_by_id(name_fld);

  src_leaf = get_leaf(element_nod.src);

  //if (element_son == null) {
    //return;
  //}

  //if (element_son.style == null) {
    //return;
  //}

  // expand the node
  if (expand!=0) {
    element_son.style.display = "block";

    element_nod.src = image_directory + get_leaf(element_nod.src).replace("_pl", "_mi");

    element_fle.src = image_directory + get_leaf(element_fle.src).replace("_cl_", "_op_");
    element_fmi.src = image_directory + get_leaf(element_fmi.src).replace("_cl_", "_op_");
    element_fri.src = image_directory + get_leaf(element_fri.src).replace("_cl_", "_op_");

    element_fmc.style.background = "#808080";
    // element_fld.value = "+";
    element_nol.alt = "Collapse";
    element_nod.alt = "Collapse";
    element_nor.alt = "Collapse";
  } else {
    element_son.style.display = "none";

    element_nod.src = image_directory + get_leaf(element_nod.src).replace("_mi", "_pl");

    element_fle.src = image_directory + get_leaf(element_fle.src).replace("_op_", "_cl_");
    element_fmi.src = image_directory + get_leaf(element_fmi.src).replace("_op_", "_cl_");
    element_fri.src = image_directory + get_leaf(element_fri.src).replace("_op_", "_cl_");

    element_fmc.style.background = "";
    // element_fld.value = "-";
    element_nol.alt = "Expand";
    element_nod.alt = "Expand";
    element_nor.alt = "Expand";
  }

  update_status(node_id);

  //for (var i = get_r_total(table_id); --i >= 0; ) {
    t_deselect(node_id);
  //}

  //t_deselect(table_id, node_id);
}



//<img id="t_1_2_n_l" onclick="tog_t_menu('1','2')" onmouseup="t_release_compo('1','2');" onmousedown="t_press_compo('1','2');" onmouseover="t_select('1','2')" onmouseout="t_deselect('1','2')" src="file:///home/sk/sk-chang/python/mypkb/img/t_n_l.png" alt="Collapse">

//function bindFunc(table_id, node_id, expand) {
function bindFuncs() {
	var signs = document.getElementsByClassName("plus_sign");
	for(var i=0;i<signs.length;i++) {
		signs[i].onclick = tog_t_menu;
		signs[i].onmouseup = t_release_compo;
		signs[i].onmousedown = t_press_compo;
		signs[i].onmouseover = t_select;
		//signs[i].onmouseout = t_deselect;
		signs[i].onmouseout = function () {t_deselect(this.id.substring(0,this.id.indexOf('_')))}
	}
}

/*
function out() {
         this.style.backgroundColor='#ffffff'
}
*/	

